//
//  Find_activityClassificationCell.m
//  SoundPill
//
//  Created by 宋旭东 on 16/5/25.
//  Copyright © 2016年 宋旭东. All rights reserved.
//
#define Start_X 5.0f           // 第一个按钮的X坐标
#define Start_Y 5.0f           // 第一个按钮的Y坐标
#define Width_Space 10.0f        // 2个按钮之间的横间距
#define Height_Space 10.0f      // 竖间距
//#define Button_Height self.    // 高
#define Button_Width 75.0f      // 宽
#import "Find_activityClassificationCell.h"
#import "Find_Activity_Top_Model.h"
#import "UIViewExt.h"
#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"
@implementation Find_activityClassificationCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier count:(NSInteger)count
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    NSLog(@"%d",count);
    if (self) {
//        self.backgroundColor = [UIColor grayColor];
        [self createSubviewsWithCount:count];
    }
    return self;
}
- (void)createSubviewsWithCount:(NSInteger)count
{
    self.banner = [[UIImageView alloc] initWithFrame:CGRectZero];
    [_banner setContentScaleFactor:[[UIScreen mainScreen] scale]];
    _banner.contentMode = UIViewContentModeScaleAspectFill;
    _banner.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    _banner.clipsToBounds = YES;
    [self addSubview:_banner];
    
    self.bannerBackgroundView = [[UIImageView alloc] initWithFrame:CGRectZero];
    _bannerBackgroundView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.251];
    [self addSubview:_bannerBackgroundView];
    
    self.bannerLable = [[UILabel alloc] initWithFrame:CGRectZero];
    _bannerLable.numberOfLines = 0;
    _bannerLable.textColor = [UIColor whiteColor];
    _bannerLable.font = [UIFont systemFontOfSize:23];
    [self addSubview:_bannerLable];
    
    self.bannerTypeTimeBGIView = [[UIImageView alloc] initWithFrame:CGRectZero];
    _bannerTypeTimeBGIView.image = [UIImage imageNamed:@"find_Labx_Tag_Black"];
    [self addSubview:_bannerTypeTimeBGIView];
    
    self.bannerTypeTimeLable = [[UILabel alloc] initWithFrame:CGRectZero];
    _bannerTypeTimeLable.font = [UIFont systemFontOfSize:15];
    _bannerTypeTimeLable.textColor = [UIColor whiteColor];
    _bannerTypeTimeLable.textAlignment = NSTextAlignmentCenter;
    _bannerTypeTimeLable.backgroundColor = [UIColor clearColor];
    [self addSubview:_bannerTypeTimeLable];
    
    self.bannerDateLable = [[UILabel alloc] initWithFrame:CGRectZero];
    _bannerDateLable.font = [UIFont boldSystemFontOfSize:17];
    _bannerDateLable.textAlignment = NSTextAlignmentRight;
    _bannerDateLable.textColor = [UIColor whiteColor];
    [self addSubview:_bannerDateLable];
    
    self.bannerWorkPriceLable = [[UILabel alloc] initWithFrame:CGRectZero];
    _bannerWorkPriceLable.font = [UIFont systemFontOfSize:15];
    _bannerWorkPriceLable.backgroundColor = [UIColor colorWithRed:0.898 green:0.000 blue:0.000 alpha:1.00];
    _bannerWorkPriceLable.textColor = [UIColor whiteColor];
    _bannerWorkPriceLable.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_bannerWorkPriceLable];
//    self.triangle = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Find_triangle"]];
//    [self addSubview:_triangle];
    
    self.bannerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_bannerButton];
    
    self.listView = [[UIScrollView alloc] initWithFrame:CGRectZero];
//    _listView.pagingEnabled = YES;
    _listView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_listView];
    for (int i = 0; i < count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor whiteColor];
        button.tag = 10 + i;
       
        [_listView addSubview:button];
        UILabel *userName = [[UILabel alloc] initWithFrame:CGRectZero];
        userName.font = [UIFont systemFontOfSize:13];
        userName.tag = 30 + i;
        userName.textAlignment = NSTextAlignmentCenter;
        [_listView addSubview:userName];
        UILabel *priceLable = [[UILabel alloc] initWithFrame:CGRectZero];
        priceLable.font = [UIFont systemFontOfSize:13];
        priceLable.textAlignment = NSTextAlignmentCenter;
        priceLable.textColor = [UIColor colorWithRed:0.875 green:0.302 blue:0.251 alpha:1.00];
        priceLable.tag = 50 + i;
        [_listView addSubview:priceLable];
    }
}
- (void)layoutSubviews
{
    [super layoutSubviews];
//    NSLog(@"%@",self.dataArray);
    Find_Activity_Top_Model *model = _dataArray[0];
    
    _banner.frame = CGRectMake(0, 0, self.bounds.size.width, 200);
    [_banner setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    
    _bannerBackgroundView.frame = _banner.bounds;
    _bannerButton.frame = _banner.bounds;
    [_bannerButton addTarget:self action:@selector(bannerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _bannerLable.frame = CGRectMake(0, 0, self.width, _banner.height);
    _bannerLable.textAlignment = NSTextAlignmentCenter;
    _bannerLable.text = model.title;
    
    _bannerTypeTimeLable.text = [NSString stringWithFormat:@"%@ | 共 %@ 条",[self switchType:model.type_id],model.comptime];
    NSDictionary *attribute = @{NSFontAttributeName: _bannerTypeTimeLable.font};
    CGSize retSize = [_bannerTypeTimeLable.text boundingRectWithSize:CGSizeMake(_banner.width / 2, 40) options:
                      NSStringDrawingTruncatesLastVisibleLine |
                      NSStringDrawingUsesLineFragmentOrigin |
                      NSStringDrawingUsesFontLeading
                                     attributes:attribute
                                        context:nil].size;
    _bannerTypeTimeLable.frame = CGRectMake(0, 20, retSize.width + 20, retSize.height + 10);
    _bannerTypeTimeBGIView.frame = CGRectMake(0, 20, _bannerTypeTimeLable.width + 10, _bannerTypeTimeLable.height);
    
    _bannerDateLable.frame = CGRectMake(0, 20, _banner.width - 20, _bannerTypeTimeLable.height);
    _bannerDateLable.text = model.comp_time;
    
    
    _bannerWorkPriceLable.text = [NSString stringWithFormat:@"%@RMB", model.price];
    NSDictionary *priceAttribute = @{NSFontAttributeName: _bannerWorkPriceLable.font};
    CGSize priceRetSize = [_bannerWorkPriceLable.text boundingRectWithSize:CGSizeMake(_banner.width / 2, 40) options:
        NSStringDrawingTruncatesLastVisibleLine |
        NSStringDrawingUsesLineFragmentOrigin |
        NSStringDrawingUsesFontLeading
        attributes:priceAttribute
        context:nil].size;
    _bannerWorkPriceLable.frame = CGRectMake(_banner.right - priceRetSize.width - 20, _banner.bottom - priceRetSize.height - 20, priceRetSize.width + 10, priceRetSize.height + 10);
    
    
    _listView.frame = CGRectMake(0, _banner.bottom, self.width, self.height - _banner.height);
    _listView.contentSize = CGSizeMake(_dataArray.count * (self.width - 40) / 3 + (_dataArray.count) * Start_X, 0);
    
    for (int i = 0; i < _dataArray.count; i++) {
//        NSInteger index = i % 3;
//        NSInteger page = i / 3;
        UIButton *button = (UIButton *)[self viewWithTag:10 + i];
        button.frame = CGRectZero;
        UILabel *username = (UILabel *)[self viewWithTag:30 + i];
        username.frame = CGRectZero;
        UILabel *priceLable = (UILabel *)[self viewWithTag:50 + i];
        priceLable.frame = CGRectZero;
    }
    for (int i = 0; i < _dataArray.count; i++) {
        UIButton *button = (UIButton *)[self viewWithTag:10 + i];
//        if (i > 2) {
//            button.frame = CGRectMake(i * ((self.width - 40) / 3 + 10) + 20, Start_Y,  (self.width - 40) / 3, (self.width - 40) / 3);
//        } else {
            button.frame = CGRectMake(i * ((self.width - 40) / 3 + Start_X) + Start_X, Start_Y, (self.width - 40) / 3, (self.width - 40) / 3);
//        }
        UILabel *username = (UILabel *)[self viewWithTag:30 + i];
        UILabel *priceLable = (UILabel *)[self viewWithTag:50 + i];
        if (i < _dataArray.count - 1) {
            Find_Activity_Top_Model *model = _dataArray[i + 1];
            [button setImageWithURL:[NSURL URLWithString:model.cover] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
            
             [button addTarget:self action:@selector(listviewButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            username.frame = CGRectMake(button.left, button.bottom, button.width, 20);
            username.text = model.title;
            priceLable.frame = CGRectMake(button.left, username.bottom, button.width, 20);
            priceLable.text = [NSString stringWithFormat:@"¥%.2f",[model.price floatValue]];
        } else {
            [button setImage:[UIImage imageNamed:@"Find_labx_more"] forState:UIControlStateNormal];
            username.text = nil;
            priceLable.text = nil;
            [button removeTarget:self action:@selector(listviewButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [button addTarget:self action:@selector(listviewMoreButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}
- (void)bannerBtnClick:(UIButton *)sender
{
    Find_Activity_Top_Model *model = _dataArray[0];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"gotoPlayer" object:model.id];
}
- (void)listviewButtonClick:(UIButton *)sender
{
    NSInteger index = sender.tag - 10;
    if (_dataArray.count > 1) {
        Find_Activity_Top_Model *model = _dataArray[index + 1];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"gotoPlayer" object:model.id];
    }
}
- (void)listviewMoreButtonClick:(UIButton *)sender
{
    Find_Activity_Top_Model *model = _dataArray[0];
    
    _block(model.type_id);
}
- (NSString *)switchType:(NSString *)type_id
{
    NSString *type_name;
    switch (type_id.intValue) {
        case 1:
            type_name = @"唱片";
            break;
        case 2:
            type_name = @"周边";
            break;
        case 3:
            type_name = @"线下活动";
            break;
        case 4:
            type_name = @"生活方式";
            break;
        default:
            break;
    }
    return type_name;
}
@end
